import { group } from 'k6';
import http from 'k6/http';

export function vulnerabilityManagementDashboard(host, headers, tags) {
    group('vulnerability management dashboard', function () {
        http.get(
            `${host}/v1/imageintegrations`,
            { headers, tags }
        );

        http.post(
            `${host}/api/graphql?opname=cvesCount`,
            JSON.stringify({
                "operationName":"cvesCount",
                "variables":{
                    "query":"Fixable:true"
                },
                "query":`query cvesCount($query: String) {
  imageVulnerabilityCount
  fixableImageVulnerabilityCount: imageVulnerabilityCount(query: $query)
  nodeVulnerabilityCount
  fixableNodeVulnerabilityCount: nodeVulnerabilityCount(query: $query)
  clusterVulnerabilityCount
  fixableClusterVulnerabilityCount: clusterVulnerabilityCount(query: $query)
}`
            }),
            { headers, tags }
        );

        http.post(
            `${host}/api/graphql?opname=getNodes`,
            JSON.stringify({
                "operationName":"getNodes",
                "variables":{
                    "query":""
                },
                "query":`query getNodes($query: String) {
  nodeCount(query: $query)
}`
            }),
            { headers, tags }
        );

        http.post(
            `${host}/api/graphql?opname=getImages`,
            JSON.stringify({
                "operationName":"getImages",
                "variables":{
                    "query":""
                },
                "query":`query getImages($query: String) {
  imageCount(query: $query)
}`
            }),
            { headers, tags }
        );

        http.post(
            `${host}/api/graphql?opname=topRiskyDeployments`,
            JSON.stringify({
                "operationName":"topRiskyDeployments",
                "variables":{
                    "query":"",
                    "vulnQuery":"",
                    "entityPagination":{
                        "offset":0,
                        "limit":25,
                        "sortOption":{
                            "field":"Deployment Risk Priority",
                            "reversed":false
                        }
                    },
                    "vulnPagination":{
                        "offset":0,
                        "limit":50,
                        "sortOption":{
                            "field":"CVSS",
                            "reversed":true
                        }
                    }
                },
                "query":`query topRiskyDeployments($query: String, $vulnQuery: String, $entityPagination: Pagination, $vulnPagination: Pagination) {
  results: deployments(query: $query, pagination: $entityPagination) {
    id
    name
    clusterName
    namespaceName: namespace
    priority
    plottedVulns: plottedImageVulnerabilities(query: $vulnQuery) {
      basicVulnCounter: basicImageVulnerabilityCounter {
        all {
          total
          fixable
          __typename
        }
        __typename
      }
      vulns: imageVulnerabilities(pagination: $vulnPagination) {
        ...vulnFields
        __typename
      }
      __typename
    }
    __typename
  }
}


fragment vulnFields on ImageVulnerability {
  id
  cve
  cvss
  severity
  __typename
}`
            }),
            { headers, tags }
        );

        http.post(
            `${host}/api/graphql?opname=topRiskiestImageVulns`,
            JSON.stringify({
                "operationName":"topRiskiestImageVulns",
                "variables":{
                    "query":"",
                    "pagination":{
                        "offset":0,
                        "limit":8,
                        "sortOption":{
                            "field":"Image Risk Priority",
                            "reversed":false
                        }
                    }
                },
                "query":`query topRiskiestImageVulns($query: String, $pagination: Pagination) {
  results: images(query: $query, pagination: $pagination) {
    id
    name {
      fullName
      __typename
    }
    vulnCounter: imageVulnerabilityCounter {
      all {
        total
        fixable
        __typename
      }
      low {
        total
        fixable
        __typename
      }
      moderate {
        total
        fixable
        __typename
      }
      important {
        total
        fixable
        __typename
      }
      critical {
        total
        fixable
        __typename
      }
      __typename
    }
    priority
    scan {
      scanTime
      __typename
    }
    __typename
  }
}`
            }),
            { headers, tags }
        );

        http.post(
            `${host}/api/graphql?opname=recentlyDetectedImageVulnerabilities`,
            JSON.stringify({
                "operationName":"recentlyDetectedImageVulnerabilities",
                "variables":{
                    "query":"CVE Type:IMAGE_CVE",
                    "scopeQuery":"",
                    "pagination":{
                        "offset":0,
                        "limit":8,
                        "sortOption":{
                            "field":"CVE Created Time",
                            "reversed":true
                        }
                    }
                },
                "query":`query recentlyDetectedImageVulnerabilities($query: String, $scopeQuery: String, $pagination: Pagination) {
  results: imageVulnerabilities(query: $query, pagination: $pagination) {
    id
    cve
    cvss
    scoreVersion
    deploymentCount
    imageCount
    isFixable(query: $scopeQuery)
    envImpact
    createdAt
    summary
    __typename
  }
}`
            }),
            { headers, tags }
        );

        http.post(
            `${host}/api/graphql?opname=mostCommonImageVulnerabilities`,
            JSON.stringify({
                "operationName":"mostCommonImageVulnerabilities",
                "variables":{
                    "query":"",
                    "vulnPagination":{
                        "offset":0,
                        "limit":15,
                        "sortOption":{
                            "field":"Deployment Count",
                            "reversed":true
                        }
                    }
                },
                "query":`query mostCommonImageVulnerabilities($query: String, $vulnPagination: Pagination) {
  results: imageVulnerabilities(query: $query, pagination: $vulnPagination) {
    id
    cve
    cvss
    scoreVersion
    isFixable
    deploymentCount
    summary
    imageCount
    lastScanned
    __typename
  }
}`
            }),
            { headers, tags }
        );

        http.post(
            `${host}/api/graphql?opname=clustersWithMostClusterVulnerabilities`,
            JSON.stringify({
                "operationName":"clustersWithMostClusterVulnerabilities",
                "variables":{},
                "query":`query clustersWithMostClusterVulnerabilities {
  results: clusters {
    id
    name
    isGKECluster
    isOpenShiftCluster
    clusterVulnerabilityCount
    clusterVulnerabilities {
      cve
      isFixable
      vulnerabilityType
      vulnerabilityTypes
      __typename
    }
    __typename
  }
}`
            }),
            { headers, tags }
        );

        http.post(
            `${host}/api/graphql?opname=topRiskyNamespaces`,
            JSON.stringify({
                "operationName":"topRiskyNamespaces",
                "variables":{
                    "query":"",
                    "vulnQuery":"",
                    "entityPagination":{
                        "offset":0,
                        "limit":25,
                        "sortOption":{
                            "field":"Namespace Risk Priority",
                            "reversed":false
                        }
                    },
                    "vulnPagination":{
                        "offset":0,
                        "limit":50,
                        "sortOption":{
                            "field":"CVSS",
                            "reversed":true
                        }
                    }
                },
                "query":`query topRiskyNamespaces($query: String, $vulnQuery: String, $entityPagination: Pagination, $vulnPagination: Pagination) {
  results: namespaces(query: $query, pagination: $entityPagination) {
    metadata {
      clusterName
      name
      id
      priority
      __typename
    }
    plottedVulns: plottedImageVulnerabilities(query: $vulnQuery) {
      basicVulnCounter: basicImageVulnerabilityCounter {
        all {
          total
          fixable
          __typename
        }
        __typename
      }
      vulns: imageVulnerabilities(pagination: $vulnPagination) {
        ...vulnFields
        __typename
      }
      __typename
    }
    __typename
  }
}

fragment vulnFields on ImageVulnerability {
  id
  cve
  cvss
  severity
  __typename
}`
            }),
            { headers, tags }
        );

        http.post(
            `${host}/api/graphql?opname=topRiskyImages`,
            JSON.stringify({
                "operationName":"topRiskyImages",
                "variables":{
                    "query":"",
                    "vulnQuery":"",
                    "entityPagination":{
                        "offset":0,
                        "limit":25,
                        "sortOption":{
                            "field":"Image Risk Priority",
                            "reversed":false
                        }
                    },
                    "vulnPagination":{
                        "offset":0,
                        "limit":50,
                        "sortOption":{
                            "field":"CVSS",
                            "reversed":true
                        }
                    }
                },
                "query":`query topRiskyImages($query: String, $vulnQuery: String, $entityPagination: Pagination, $vulnPagination: Pagination) {
  results: images(query: $query, pagination: $entityPagination) {
    id
    name {
      fullName
      __typename
    }
    priority
    plottedVulns: plottedImageVulnerabilities(query: $vulnQuery) {
      basicVulnCounter: basicImageVulnerabilityCounter {
        all {
          total
          fixable
          __typename
        }
        __typename
      }
      vulns: imageVulnerabilities(pagination: $vulnPagination) {
        ...vulnFields
        __typename
      }
      __typename
    }
    __typename
  }
}

fragment vulnFields on ImageVulnerability {
  id
  cve
  cvss
  severity
  __typename
}`
            }),
            { headers, tags }
        );

        http.post(
            `${host}/api/graphql?opname=topRiskyNodes`,
            JSON.stringify({
                "operationName":"topRiskyNodes",
                "variables":{
                    "query":"",
                    "vulnQuery":"",
                    "entityPagination":{
                        "offset":0,
                        "limit":25,
                        "sortOption":{
                            "field":"Node Risk Priority",
                            "reversed":false
                        }
                    },
                    "vulnPagination":{
                        "offset":0,
                        "limit":50,
                        "sortOption":{
                            "field":"CVSS",
                            "reversed":true
                        }
                    }
                },
                "query":`query topRiskyNodes($query: String, $vulnQuery: String, $entityPagination: Pagination, $vulnPagination: Pagination) {
  results: nodes(query: $query, pagination: $entityPagination) {
    id
    name
    clusterName
    priority
    plottedVulns: plottedNodeVulnerabilities(query: $vulnQuery) {
      basicVulnCounter: basicNodeVulnerabilityCounter {
        all {
          total
          fixable
          __typename
        }
        __typename
      }
      vulns: nodeVulnerabilities(pagination: $vulnPagination) {
        ...vulnFields
        __typename
      }
      __typename
    }
    __typename
  }
}

fragment vulnFields on NodeVulnerability {
  id
  cve
  cvss
  severity
  __typename
}`
            }),
            { headers, tags }
        );

        http.post(
            `${host}/api/graphql?opname=topRiskiestNodeComponents`,
            JSON.stringify({
                "operationName":"topRiskiestNodeComponents",
                "variables":{
                    "query":"",
                    "pagination":{
                        "offset":0,
                        "limit":8,
                        "sortOption":{
                            "field":"Component Risk Priority",
                            "reversed":false
                        }
                    }
                },
                "query":`query topRiskiestNodeComponents($query: String, $pagination: Pagination) {
  results: nodeComponents(query: $query, pagination: $pagination) {
    id
    name
    version
    lastScanned
    vulnCounter: nodeVulnerabilityCounter {
      all {
        total
        fixable
        __typename
      }
      low {
        total
        fixable
        __typename
      }
      moderate {
        total
        fixable
        __typename
      }
      important {
        total
        fixable
        __typename
      }
      critical {
        total
        fixable
        __typename
      }
      __typename
    }
    priority
    __typename
  }
}`
            }),
            { headers, tags }
        );

        http.post(
            `${host}/api/graphql?opname=topRiskiestImageComponents`,
            JSON.stringify({
                "operationName":"topRiskiestImageComponents",
                "variables":{
                    "query":"",
                    "pagination":{
                        "offset":0,
                        "limit":8,
                        "sortOption":{
                            "field":"Component Risk Priority",
                            "reversed":false
                        }
                    }
                },
                "query":`query topRiskiestImageComponents($query: String, $pagination: Pagination) {
  results: imageComponents(query: $query, pagination: $pagination) {
    id
    name
    version
    lastScanned
    vulnCounter: imageVulnerabilityCounter {
      all {
        total
        fixable
        __typename
      }
      low {
        total
        fixable
        __typename
      }
      moderate {
        total
        fixable
        __typename
      }
      important {
        total
        fixable
        __typename
      }
      critical {
        total
        fixable
        __typename
      }
      __typename
    }
    priority
    __typename
  }
}`
            }),
            { headers, tags }
        );

        http.post(
            `${host}/api/graphql?opname=topRiskiestNodeVulns`,
            JSON.stringify({
                "operationName":"topRiskiestNodeVulns",
                "variables":{
                    "query":"",
                    "pagination":{
                        "offset":0,
                        "limit":8,
                        "sortOption":{
                            "field":"Node Risk Priority",
                            "reversed":false
                        }
                    }
                },
                "query":`query topRiskiestNodeVulns($query: String, $pagination: Pagination) {
  results: nodes(query: $query, pagination: $pagination) {
    id
    name
    vulnCounter: nodeVulnerabilityCounter {
      all {
        total
        fixable
        __typename
      }
      low {
        total
        fixable
        __typename
      }
      moderate {
        total
        fixable
        __typename
      }
      important {
        total
        fixable
        __typename
      }
      critical {
        total
        fixable
        __typename
      }
      __typename
    }
    priority
    scan {
      scanTime
      __typename
    }
    __typename
  }
}`
            }),
            { headers, tags }
        );
    });
}
